var requests = require('../../requests/api.js');
var util = require('../../utils/util.js');

let app = getApp()
// designerGroup.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //没有更多
    loading: true, //加载框是否显示
    searchLoading: false, //把"上拉加载"的变量设为true，显示  
    searchLoadingComplete: false, //把“没有数据”设为false，隐藏  
    page: 1, //分页数据，从第一页开始
    text_line: 3,
    isOpen: false,
    down_img: "../../assets/base_icon/down.png",
    goods: [

    ],
    name: '',
    desc: '',
    logoImg: '',
    designerId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // "id": 657,
    // "country": null,
    // "type": "brand",
    // "name": "XD Design",
    // "img": "https://img.wowdsgn.com/brand/logo/0c1a9aee-021e-481b-a2e5-7e8ff6ad18ac_2dimension_800x800.jpg",
    // "desc
    new app.WeToast();
    this.setData({
      designerId: options.designerId
    })
    this.request_designerData(this.data.page)
    this.request_designerDesc()
    console.log("--------------------" + options.designerId)
  },
  //NET 

  request_designerData: function(page) {
    //网络请求的时候先把加载框加出来
    const self = this;
    self.setData({
      loading: false,
    });

    var params = {
      designerId: this.data.designerId,
      currentPage: page,
      pageSize: 10
    };

    requests.getDesignerProducts(params, (res) => {
      if (!res.data.productVoList) return;
      if (res.data.productVoList.length == 0) { //没有数据
        self.setData({
          searchLoadingComplete: true, //把“没有数据”设为true，显示

        })
      } else { //说明还有数据
        self.setData({
          searchLoadingComplete: false, //把“没有数据”设为false，隐藏  

        })
      };
      self.setData({
        loading: true, //隐藏加载框
        goods: self.data.goods.concat(res.data.productVoList) //给数组赋值
      })

    }, (data) => {
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }
      self.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示
        loading: true //隐藏加载框
      })

    }, () => {
      self.setData({
        searchLoading: false //把"上拉加载"的变量设为false，隐藏  

      })
    });
  },

  request_designerDesc: function() {
    const self = this

    requests.getDesignerDetail(self.data.designerId, (res) => {
      self.setData({
        //bannerLinkType 6商品详情 8专题
        name: res.data.designerName,
        desc: res.data.designerDesc,
        logoImg: res.data.designerPhoto
      })
    }, (data) => {
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.moreData()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //上拉触底
  moreData: function() {
    let that = this;
    //如果还有数据就继续加载
    if (!that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        page: that.data.page + 1
      })
      that.setData({
        searchLoading: true //把"上拉加载"的变量设为false，隐藏

      })

      that.request_designerData(that.data.page)

    }
  },
  //MARK: --privite method
  down_up_click: function() {
    var openState = this.data.isOpen;
    var number = this.data.text_line;
    var imgPath = this.data.down_img;
    if (openState) {
      number = 3
      imgPath = "../../assets/base_icon/down.png"
    } else {
      number = 0
      imgPath = "../../assets/base_icon/up.png"
    };
    openState = !openState;

    this.setData({
      isOpen: openState,
      text_line: number,
      down_img: imgPath
    })
  },
  toDetailsTap: function(e) {
    var id = e.currentTarget.dataset.productid;
    console.log("---------------" + id)
    wx.navigateTo({
      url: '../product/productDetail?productId=' + id,
    })
  },
})